package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"news_manage_sys/models"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Create() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var n models.News
	n.Title = c.GetString("title")
	n.TypeId, _ = c.GetInt("tid")
	n.Data = c.GetString("data")
	if n.Title == "" || n.TypeId == 0 || n.Data == "" {
		c.Data["json"] = models.Msg{
			Code: 201,
			Msg:  "传入有空值，不予添加",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if err := n.GetNewsByTitle(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if n.ID != 0 {
		c.Data["json"] = models.Msg{
			Code: 202,
			Msg:  "新闻已存在",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("news:%s", n.Title)
	JsonData, _ := json.Marshal(&n)
	if err := models.Rdb.Set(models.Ctx, key, JsonData, 1).Err(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if err := n.CreateNews(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *NewsController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	key := fmt.Sprintf("news:*%d", id)
	rkey, _ := models.Rdb.Keys(models.Ctx, key).Result()
	models.Rdb.Del(models.Ctx, rkey[0])
	models.DB.Delete(&models.News{}, id)
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *NewsController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var n models.News
	ID, _ := c.GetInt("id")
	n.ID = uint(ID)
	if err := n.GetNewsByID(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	n.Title = c.GetString("title")
	n.TypeId, _ = c.GetInt("tid")
	n.Data = c.GetString("data")
	if n.ID == 0 {
		c.Data["json"] = models.Msg{
			Code: 202,
			Msg:  "新闻不存在",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("news:%s,id:%d", n.ID)
	JsonData, _ := json.Marshal(&n)
	if err := models.Rdb.Set(models.Ctx, key, JsonData, 1).Err(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if err := n.UpdateNews(); err != nil {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *NewsController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var keys []string
	var news []models.News
	if keys, _ = models.Rdb.Keys(models.Ctx, "news:*").Result(); len(keys) == 0 {
		if err := models.DB.Find(&news).Error; err != nil {
			c.Data["json"] = models.Msg{
				Code: 500,
				Msg:  "服务器繁忙",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
		for _, n := range news {
			key := fmt.Sprintf("news:%s,Id:%d", n.Title, n.ID)
			JsonData, _ := json.Marshal(&n)
			if err := models.Rdb.Set(models.Ctx, key, JsonData, -1).Err(); err != nil {
				c.Data["json"] = models.Msg{
					Code: 500,
					Msg:  "服务器繁忙",
					Data: nil,
				}
				c.ServeJSON()
				return
			}
		}
	} else {
		for _, key := range keys {
			JsonNews, _ := models.Rdb.Get(models.Ctx, key).Result()
			var News models.News
			json.Unmarshal([]byte(JsonNews), &News)
			news = append(news, News)
		}
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "获取成功",
		Data: news,
	}
	c.ServeJSON()
	return
}
